package com.yenlien.traceyun.business.service;

import com.yenlien.traceyun.business.dao.CuringDao;
import com.yenlien.traceyun.business.dao.StorageCuringDao;
import com.yenlien.traceyun.business.entity.Curing;
import com.yenlien.traceyun.business.entity.StorageCuring;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by LiuSha on 2020/7/27.
 */
@Service
public class StorageCuringService {
    @Autowired
    private StorageCuringDao storageCuringDao;
    @Autowired
    private CuringDao curingDao;

    public Page<StorageCuring> findByList(final StorageCuring storageCuring) {
        Pageable pageable = PageRequest.of(storageCuring.getPage(), storageCuring.getPageSize(), Sort.Direction.DESC, "createtime");
        return storageCuringDao.findAll(new Specification<StorageCuring>() {
            private static final long serialVersionUID = 1L;

            @Override
            public Predicate toPredicate(Root<StorageCuring> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Path<String> companyId = root.get("companyId");
                //构造查询条件
                List<Predicate> list = new ArrayList<>();
                List<Predicate> or = new ArrayList<>();
                if (!StringUtils.isEmpty(storageCuring.getCompanyId())) {
                    list.add(cb.equal(companyId, storageCuring.getCompanyId()));
                }
                Predicate[] predicate = new Predicate[list.size()];
                Predicate pre_and = cb.and(list.toArray(predicate));
                if (!StringUtils.isEmpty(storageCuring.getQuery())) {
                    or.add(cb.like(root.get("name"), "%" + storageCuring.getQuery() + "%"));
                    or.add(cb.like(root.get("technology"), "%" + storageCuring.getQuery() + "%"));
                } else {
                    return pre_and;
                }
                Predicate[] pre_or = new Predicate[or.size()];
                Predicate ora = cb.or(or.toArray(pre_or));
                return query.where(pre_and, ora).getRestriction();
            }
        }, pageable);
    }

    @Transactional
    public void save(List<StorageCuring> storageCuringList, String companyId) {
        // 一个养护详情可绑定多个养护
        List<Curing> curingList = new ArrayList<>();
        for (StorageCuring storageCuring : storageCuringList) {
            storageCuring.setCompanyId(companyId);
            storageCuringDao.save(storageCuring);
            if (!CollectionUtils.isEmpty(storageCuring.getCuringList())) {
                curingList.addAll(storageCuring.getCuringList());
            }
        }
        if (!CollectionUtils.isEmpty(curingList)) {
            for (StorageCuring storageCuring : storageCuringList) {
                for (Curing curing : curingList) {
                    if (StringUtils.isEmpty(curing.getId())) {
                        // 添加
                        Curing curing1 = new Curing();
                        BeanUtils.copyProperties(curing, curing1);
                        curing1.setId(null);
                        curing1.setCuringId(storageCuring.getId());
                        curingDao.save(curing1);
                    } else {
                        // 修改
                        curingDao.save(curing);
                    }
                }
            }
        }
    }


    @Transactional
    public void plantSave(StorageCuring storageCuring) {
        storageCuringDao.save(storageCuring);
        if (storageCuring.getCuringList() != null && storageCuring.getCuringList().size() > 0) {
            for (Curing curing : storageCuring.getCuringList()) {
                curing.setCuringId(storageCuring.getId());
                curingDao.save(curing);
            }
        }
    }


    @Transactional
    public void update(StorageCuring storageCuring) {
        storageCuringDao.save(storageCuring);
        deleteCuring(curingDao.findByCuringId(storageCuring.getId()));
        if (storageCuring.getCuringList() != null && storageCuring.getCuringList().size() > 0) {
            for (Curing curing : storageCuring.getCuringList()) {
                curing.setCuringId(storageCuring.getId());
                curingDao.save(curing);
            }
        }
    }

    private void deleteCuring(List<Curing> curingList) {
        if (curingList != null && curingList.size() > 0) {
            curingDao.deleteAll(curingList);
        }
    }

    public StorageCuring findById(String id) {
        StorageCuring storageCuring = storageCuringDao.findById(id);
        storageCuring.setCuringList(curingDao.findByCuringId(id));
        return storageCuring;
    }

    public List<StorageCuring> findByAll(String companyId) {
        return storageCuringDao.findByCompanyId(companyId);
    }

    @Transactional
    public void delete(String id) {
        storageCuringDao.deleteById(id);
        deleteCuring(curingDao.findByCuringId(id));
    }

}
